﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.CApAPPAnalytics
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Tecnomatix.DnSoftwareAnalyticsServices;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands.AutomaticPathPlanner;

internal class CApAPPAnalytics
{
  private static readonly string APPTypePrefix = "Automatic Path Planner";
  private Stopwatch m_globalStopWatch = new Stopwatch();
  private bool m_isObjectFlowOperation;

  public CApAPPAnalytics()
  {
    this.HasMagnetPointBeenUsed = false;
    this.m_globalStopWatch.Start();
  }

  public bool HasMagnetPointBeenUsed { get; set; }

  public TxSoftwareAnalyticsServicesMsg CreateMessage(string uniqueId)
  {
    return new TxSoftwareAnalyticsServicesMsg()
    {
      Type = $"{CApAPPAnalytics.APPTypePrefix} - {uniqueId}"
    };
  }

  public void LogMessage(TxSoftwareAnalyticsServicesMsg message)
  {
    if (message == null)
      return;
    TxSoftwareAnalyticsServices.log(message);
  }

  public void LogElapsedTime()
  {
    TxSoftwareAnalyticsServicesMsg message = this.CreateMessage("Elapsed Time");
    message.addProperty("Operation Type", this.m_isObjectFlowOperation ? "Object Flow" : "Robotic");
    message.addNumericProperty("Elapsed Time", this.m_globalStopWatch.Elapsed.TotalSeconds);
    this.LogMessage(message);
  }

  public void LogSelectedOperations(TxObjectList selectedOperations)
  {
    if (selectedOperations == null || ((Collection<ITxObject>) selectedOperations).Count <= 0)
      return;
    TxSoftwareAnalyticsServicesMsg message = this.CreateMessage("Input Operations");
    message.addNumericProperty("Operation Count", (double) ((Collection<ITxObject>) selectedOperations).Count);
    if (((Collection<ITxObject>) selectedOperations)[0] is ITxOperation selectedOperation)
    {
      int num1;
      switch (selectedOperation)
      {
        case TxObjectFlowOperation _:
          num1 = 1;
          break;
        case ITxRoboticOrderedCompoundOperation _:
          num1 = (selectedOperation as ITxRoboticOrderedCompoundOperation).Robot == null ? 1 : 0;
          break;
        default:
          num1 = 0;
          break;
      }
      this.m_isObjectFlowOperation = num1 != 0;
      int num2 = 0;
      if (selectedOperation is ITxObjectCollection)
        num2 = (selectedOperation as ITxObjectCollection).Count;
      string str1 = "Unknown";
      string str2 = "default";
      if (this.m_isObjectFlowOperation)
      {
        str1 = "Object Flow";
      }
      else
      {
        if (selectedOperation is TxWeldOperation)
          str1 = "Weld";
        else if (selectedOperation is TxGenericRoboticOperation)
        {
          str1 = "Generic";
          if (((TxBaseGenericRoboticOperation) (selectedOperation as TxGenericRoboticOperation)).ProcessType == "PICK_AND_PLACE_OPERATION")
            str1 = "Pick and Place";
        }
        if (selectedOperation is ITxRoboticOrderedCompoundOperation)
        {
          TxRobot robot = (selectedOperation as ITxRoboticOrderedCompoundOperation).Robot as TxRobot;
          if (TxRobot.op_Inequality(robot, (ITxObject) null))
          {
            if (robot.UsesNumericInverse)
              str1 = "Load Assist";
            if (robot.Controller != null && robot.Controller.Name != "default" && robot.Controller.Name != "")
            {
              TxRoboticControllerUtilities controllerUtilities = new TxRoboticControllerUtilities();
              bool flag = false;
              str2 = controllerUtilities.DoesRCSExistAndValid(robot, ref flag) ? "RCS Connected" : "RCS Disconnected";
            }
          }
        }
      }
      int num3 = 0;
      ITxOrderedObjectCollection objectCollection = selectedOperation as ITxOrderedObjectCollection;
      for (int index = 0; index < ((ITxObjectCollection) objectCollection).Count; ++index)
      {
        bool flag = false;
        ITxObject childAt = objectCollection.GetChildAt(index);
        switch (childAt)
        {
          case TxWeldLocationOperation _:
          case TxRoboticSeamLocationOperation _:
            ++num3;
            flag = true;
            break;
          case TxGenericRoboticLocationOperation _:
            TxGenericRoboticLocationOperation locationOperation = childAt as TxGenericRoboticLocationOperation;
            if (locationOperation.ProcessType == "PICK_OPERATION_TYPE" || locationOperation.ProcessType == "PLACE_OPERATION_TYPE")
            {
              ++num3;
              flag = true;
              break;
            }
            break;
        }
        if (!flag && (index == 0 || index == ((ITxObjectCollection) objectCollection).Count - 1))
          ++num3;
      }
      message.addProperty("Operation Type", str1);
      message.addNumericProperty("Operation Size", (double) num2);
      message.addNumericProperty("Locations of Interest", (double) num3);
      message.addProperty("Robot Controller", str2);
    }
    this.LogMessage(message);
  }

  public void LogComputationEnd(
    long elapsedTime,
    CApAPPAnalytics.ComputationType planType,
    KiAppPathPlanningParamsEx parameters,
    int zoneCount,
    double trajectoryOptimizationGain,
    CApAPPAnalytics.Status status,
    CApAPPLimitsUserControl limits,
    int fixedPointCount)
  {
    string str1 = "Unknown";
    switch (planType)
    {
      case CApAPPAnalytics.ComputationType.PathPlanning:
        str1 = "Plan";
        break;
      case CApAPPAnalytics.ComputationType.Optimization:
        str1 = "Optimize";
        break;
      case CApAPPAnalytics.ComputationType.DoBoth:
        str1 = "Plan and Optimize";
        break;
    }
    string str2 = "Unknown";
    bool flag1 = false;
    bool flag2 = false;
    if (planType == CApAPPAnalytics.ComputationType.Optimization || planType == CApAPPAnalytics.ComputationType.DoBoth)
    {
      switch ((int) parameters.OptimizationOption)
      {
        case 0:
          str2 = this.m_isObjectFlowOperation ? "Distance" : "Joint Travel";
          break;
        case 1:
          str2 = "Clearance";
          break;
        case 2:
          if (parameters.CTOMode == null)
          {
            str2 = "Zone Assignment";
          }
          else
          {
            str2 = "Full Cycle Time";
            flag2 = true;
          }
          flag1 = true;
          break;
        case 3:
          str2 = "Energy";
          flag1 = true;
          flag2 = true;
          break;
      }
    }
    TxSoftwareAnalyticsServicesMsg message1 = this.CreateMessage("Computation");
    message1.addProperty("Operation Type", this.m_isObjectFlowOperation ? "Object Flow" : "Robotic");
    message1.addNumericProperty("Elapsed Time", (double) elapsedTime / 1000.0);
    message1.addProperty("Computation Type", str1);
    message1.addProperty("Optimization Type", str2);
    message1.addProperty("Status", status == CApAPPAnalytics.Status.Success ? "Success" : "Failure");
    message1.addNumericProperty("Fixed Point Count", (double) fixedPointCount);
    this.LogMessage(message1);
    if (flag1)
    {
      TxSoftwareAnalyticsServicesMsg message2 = this.CreateMessage("Zones");
      message2.addProperty("Optimization Type", str2);
      message2.addNumericProperty("Zone Count", (double) zoneCount);
      this.LogMessage(message2);
    }
    if (flag2)
    {
      TxSoftwareAnalyticsServicesMsg message3 = this.CreateMessage("Optimization Gain");
      message3.addProperty("Optimization Type", str2);
      message3.addNumericProperty("Gain", trajectoryOptimizationGain * -100.0);
      this.LogMessage(message3);
    }
    if (!this.m_isObjectFlowOperation)
      return;
    bool? isChecked = limits.cbLimitsRx.IsChecked;
    int num;
    if (!isChecked.GetValueOrDefault())
    {
      isChecked = limits.cbLimitsRy.IsChecked;
      if (!isChecked.GetValueOrDefault())
      {
        isChecked = limits.cbLimitsRz.IsChecked;
        if (!isChecked.GetValueOrDefault())
        {
          isChecked = limits.cbLimitsTx.IsChecked;
          if (!isChecked.GetValueOrDefault())
          {
            isChecked = limits.cbLimitsTy.IsChecked;
            if (!isChecked.GetValueOrDefault())
            {
              isChecked = limits.cbLimitsTz.IsChecked;
              num = isChecked.GetValueOrDefault() ? 1 : 0;
              goto label_26;
            }
          }
        }
      }
    }
    num = 1;
label_26:
    bool flag3 = num != 0;
    TxSoftwareAnalyticsServicesMsg message4 = this.CreateMessage("Object Flow Parameters");
    message4.addProperty("Magnet Point", this.HasMagnetPointBeenUsed ? "true" : "false");
    message4.addProperty("Movement Limits", flag3 ? "true" : "false");
    this.LogMessage(message4);
  }

  public enum ComputationType
  {
    PathPlanning,
    Optimization,
    DoBoth,
  }

  public enum Status
  {
    Success,
    Failure,
  }
}
